<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class SaleContractRecord extends BaseController
{
    protected $title='销售合同';
    protected $table='sale_contract_record';
    protected $scType=1;
    public function index()
    {
        $param=$this->request->param();
        if (request()->isAjax()) {
            $where[]=['qo.status','>',-1];
            $where[]=['qo.type','=',$this->scType];

            if (isset($this->param['status'])&&$this->param['status']){
                $where[]=['qo.status','=',$this->param['status']];
            }
            if (isset($this->param['id'])&&$this->param['id']){
                $where[]=['qo.id','=',$this->param['id']];
            }
            if (isset($this->param['name'])&&$this->param['name']){
                $where[]=['py.name','like','%'.$this->param['name'].'%'];
            }
            if (isset($this->param['userName'])&&$this->param['userName']){
                $where[]=['ad.name','like','%'.$this->param['userName'].'%'];
            }
            if (isset($this->param['orderNum'])&&$this->param['orderNum']){
                $where[]=['qo.orderNum','like','%'.$this->param['orderNum'].'%'];
            }
            if (isset($this->param['create_time'])&&$this->param['create_time']){
                $dates=explode('~',$this->param['create_time']);
                $sTime=strtotime($dates[0]);
                $eTime=strtotime($dates[1].' 23:59:59');
                $where[]=['qo.create_time','between',[$sTime,$eTime]];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('qo')
                ->leftJoin('purveyor py','py.id=qo.punterId')
                ->leftJoin('admin ad','ad.id=qo.createId')
                ->where($where)
                ->field('qo.*,py.name as pName,ad.name as userName')
                ->order('qo.id DESC')
                ->paginate($rows);
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }
    public function info($id='')
    {
        $detail = Db::name($this->table)->where('id', $id)->find();
        $mxList=Db::name('sale_contract_record_detail')->alias('qod')
            ->leftJoin('material_dossier md','md.id=qod.mdId')
            ->where('qod.status','>',-1)
            ->where('qod.orderId',$id)
            ->field('qod.*,md.name as mdName,md.buyCode,md.brand,md.specs,md.unit,md.bar_code')
            ->select();
//        $countNum=Db::name('sale_contract_record_detail')
//            ->where('status','>',-1)
//            ->where('orderId',$id)->sum('bjNum');
//        $countPrice=Db::name('sale_contract_record_detail')
//            ->where('status','>',-1)
//            ->where('orderId',$id)->sum('bjMoney');
        $projectList=Db::name('project')->where('status','in',[1,2,3])
            ->field('id,name')->select();
        View::assign('projectList', $projectList);
        View::assign('thisDate',date('Y-m-d'));
        $payTypeList=Db::name('quo_order_pay_type')
            ->where('status',1)
            ->field('id,name')->select();
        View::assign('payTypeList',$payTypeList);
        $quoOrderTax=getOrderTax();
        View::assign('quoOrderTax',$quoOrderTax);
//        View::assign(['countPrice'=>$countPrice,'countNum'=>$countNum]);
        View::assign('detail', $detail);
        $punterList=Db::name('purveyor')->where('status',1)
            ->where('dw_type',2)
            ->field('id,name')->select();
        view::assign('punterList',$punterList);
        $saleUniList=Db::name('department')->where('status',1)->where('pid',0)->field('id,title')->select();
        view::assign('saleUniList',$saleUniList);
        View::assign('mxList', $mxList);
        $saleUserList=Db::name('admin')
//                ->where('id','in',$detail['saleId'])
            ->where('status',1)
            ->field('id,name')->select();
        view::assign('saleUserList',$saleUserList);
        view::assign('id',$id);
        return view();
    }
}